<?php
namespace app\common\model;


use app\model\CommonModel;

class RsBrand extends CommonModel
{

    /**
     * 上传品牌图标
     */
    public function uploadBrand()
    {
        try {
            $file = request()->file('file');
            $file_types = explode('.', $_FILES['file']['name']);
            $file_type = $file_types[count($file_types) - 1]; //后缀
            $attr = ['png','jpg','jpeg','pjpeg','x-png','png'];
            $file_type=strtolower($file_type);
            if (!in_array($file_type, $attr)) {
                return fail('格式错误，请上传正确的图片文件格式！');
            }
            $maxSize = 4194304;
            if ($_FILES["file"]["size"] > $maxSize) {
                return fail('文件大小不能超过4M！');
            }
            $brandImg = '';
            $params = [
                'dirName' => 'brand',
                'file'    => $file,
            ];
            $result = (new Upload())->image($params);
            $result = json_decode($result->getContent(),true);
            if ($result['data']) {
                $brandImg = $result['data']['url'];
            }
            if (empty($brandImg)) {
                return fail('上传失败');
            }
            // 上传到本地服务器
//            $savePath = date('Ymd').'/'.md5((string) microtime(true)).'/'.$file->getOriginalName();
//            $savename = \think\facade\Filesystem::disk('public')->putFileAs('brand', $file, $savePath);
//            $savename = str_replace('\\', '/', $savename);
//            $brandImg = request()->domain() . '/storage/' . $savename;
        } catch (\Exception $e) {
            return fail('上传失败：'.$e->getMessage());
        }
        return success('上传成功', [
            'url' =>$brandImg,
        ]);
    }
}
